/**
 * Project Name:gc.sms.mvn
 * File Name:MenuController.java
 * Package Name:com.clps.dev.sms.controller
 * Date:2018年11月18日上午11:05:59
 * Copyright (c) 2018, longbiosphere@163.com All Rights Reserved.
 *
 */
package com.clps.dev.sms.controller.sm;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.clps.dev.sms.common.CommonData;
import com.clps.dev.sms.common.result.ResponseResult;
import com.clps.dev.sms.common.result.ResultState;
import com.clps.dev.sms.entity.sm.Menu;
import com.clps.dev.sms.service.sm.IMenuService;
import com.clps.dev.sms.util.factory.ToolFactory;
import com.clps.dev.sms.util.tree.TreeBean;
import com.clps.dev.sms.util.tree.TreeUtil;
import com.clps.dev.sms.vo.sm.MenuVO;

/**
 * ClassName: MenuController <br/>
 * Description: date: 2018年11月18日 上午11:05:59 <br/>
 *
 * @author long.ma
 * @version V1.0
 * @since JDK 1.8
 */
@Controller
public class MenuController {

	@Resource
	private IMenuService menuService;
	Logger logger = Logger.getLogger(MenuController.class);
 
	/**
	 * addMenu: 跳转到添加界面
	 *
	 * @author long.ma
	 * @return
	 * @since JDK 1.8
	 */
	@GetMapping(value = "/menu")
	public String addMenu() {
		return "sm/menu_add";

	}

	/**
	 * 
	 * addMenu:创建新菜单
	 * 
	 * @author long.ma
	 * @param menu
	 * @return ResponseResult<Menu>
	 * @since JDK 1.8
	 */
	@PostMapping(value = "/menu")
	@ResponseBody
	public ResponseResult<Menu> addMenu(@RequestBody MenuVO menuVO) {
		Menu menu = new Menu();
		menu.setMenuName(menuVO.getMenuName());
		menu.setMenuDecription(menuVO.getMenuDecription());
		menu.setMenuUrl(menuVO.getMenuUrl());
		menu.setMenuSymbolPath(menuVO.getMenuSymbolPath());
		menu.setMenuParentId(Integer.valueOf(menuVO.getMenuParent()));
		menu.setMenuCreatedEmployeeId(1);
		menu.setMenuCreatedTime(ToolFactory.getDateInstance(CommonData.YYYY_MM_DD_hh_MM_SS));
		menu.setMenuUpdatedEmployeeId(1);

		ResponseResult<Menu> responseResult = menuService.addMenu(menu);
		return responseResult;
	}

	 
	/**
	 * editMenu: 通过菜单id获取该菜单的信息，并跳转到修改界面
	 *
	 * @author long.ma
	 * @param menuId
	 * @return
	 * @since JDK 1.8
	 */
	@GetMapping(value = "/menu/{menuId}")
	public String editMenu(@PathVariable("menuId") Integer menuId, Model model) {
		ResponseResult<Menu> responseResult = menuService.queryMenu(menuId);
		Menu menu = (Menu) responseResult.getData();
		model.addAttribute("menu", menu);
		return "sm/menu_edit";
	}

	/**
	 * editMenu: 修改菜单
	 *
	 * @author long.ma
	 * @param menu
	 * @return ResponseResult<Menu>
	 * @since JDK 1.8
	 */
	@PostMapping(value = "/menu/edit")
	@ResponseBody
	public ResponseResult<Menu> editMenu(@RequestBody Menu menu) {
		ResponseResult<Menu> responseResult = menuService.queryMenu(menu.getMenuId());
		Menu menu1 = (Menu) responseResult.getData();
		menu.setMenuCreatedEmployeeId(menu1.getMenuCreatedEmployeeId());
		menu.setMenuCreatedTime(menu1.getMenuCreatedTime());
		menu.setMenuUpdatedEmployeeId(menu1.getMenuCreatedEmployeeId());

		responseResult = menuService.editMenu(menu);
		return responseResult;

	}

	/**
	 * 
	 * @description getMainMenus 查询所有的菜单 返回左侧栏
	 * @return ResponseResult<Menu> 以json格式返回所有菜单列表
	 */

	@SuppressWarnings("unchecked")
	@PostMapping(value = "/menus/index")
	@ResponseBody
	public ResponseResult<Menu> getMainMenus() {
		ResponseResult<Menu> responseResult = menuService.queryAllMenu();

		List<Menu> menuList=(List<Menu>) responseResult.getData();
	
		String jsonString=TreeUtil.createTreeOfMenu(menuList);
		 
		responseResult.setData(jsonString);
		responseResult.setPageInfo(null);
		responseResult.setState(ResultState.OPERATIONSUCCESS);
		return responseResult;
	}
	
	@SuppressWarnings("unchecked")
	@PostMapping(value = "/menus")
	@ResponseBody
	public ResponseResult<Menu> getMenus() {
		ResponseResult<Menu> responseResult = menuService.queryAllMenu();

		List<Menu> menuList=(List<Menu>) responseResult.getData();
	
		List<TreeBean<Menu>> list=TreeUtil.createTreeOfMenuList(menuList);
		 
		responseResult.setData(list);
		responseResult.setPageInfo(null);
		responseResult.setState(ResultState.OPERATIONSUCCESS);
		return responseResult;
	}

	/**
	 * 
	 * showMenu: 查询所有的菜单信息
	 * 
	 * @author long.ma
	 * @param map
	 * @return ResponseResult<Menu>
	 * @since JDK 1.8
	 */
	@GetMapping(value = "/m/list")
	public String showMenu() {
		return "sm/menu";
	}

}
